package com.iengoo.spacewar;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief 飞船类型
 * @email 122162005@qq.com
 * @date 2018-12-26 22:08
 */
public class Craft {
    private int x;
    private int y;
    // 飞船当前贴图坐标对象
    private BufferedImage currentImage;
    private BufferedImage[] craftImages;
    private BufferedImage[] craftTipsImages;
    // 飞船的宽度以及高度，飞船看成正方形，只取贴图有效部分（非透明）的宽度和高度
    public static final int SIZE = 24;
    // 飞船当前图片坐标对象对应的下标
    private int index = 0;
    private int tipsIndex = 0;
    // 飞船旋转角度
    private double angle = 0;
    // 是否正在开火
    private boolean firing = false;
    // 如果键入的字母对应不上地雷携带的字母以及顺序，测提示
    private boolean noLetter = false;

    private List<Plasma> plasmaList;  // 线程不安全

    public Craft() throws IOException {

        craftImages = new BufferedImage[]{
                ImageIO.read(getClass().getResource("resources/craft01.png")),
                ImageIO.read(getClass().getResource("resources/craft02.png")),
                ImageIO.read(getClass().getResource("resources/craft03.png")),
                ImageIO.read(getClass().getResource("resources/craft04.png")),
                ImageIO.read(getClass().getResource("resources/craft03.png")),
                ImageIO.read(getClass().getResource("resources/craft02.png")),
                ImageIO.read(getClass().getResource("resources/craft01.png"))
        };

        craftTipsImages = new BufferedImage[]{
                ImageIO.read(getClass().getResource("resources/craft05.png")),
                ImageIO.read(getClass().getResource("resources/craft06.png")),
                ImageIO.read(getClass().getResource("resources/craft07.png"))
        };

        currentImage = craftImages[0];

        x = (Config.PANEL_WIDTH - SIZE) / 2;
        y = Config.PANEL_HEIGHT - (SIZE * 3);
        plasmaList = new CopyOnWriteArrayList<>();
    }

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.rotate(angle, x + (SIZE / 2), y + (SIZE / 2));
//        g2d.drawRect(x, y, SIZE, SIZE);
        g2d.drawImage(currentImage,
                x, y, SIZE, SIZE, null);
        g2d.rotate(-angle, x + (SIZE / 2), y + (SIZE / 2));
    }

    /**
     * 飞船切换背景
     */
    public void switchBG() {
        // 如果没有开火，则不进行后面贴图轮换的逻辑
        if (!firing) {
            noMineTips();
            return;
        }

        // 降低飞船旋转的倍数
        int freq = 2;
        index++;
        if (index % (7 * freq) == 0) {
            index = 0;
//            this.angle = 0;
            firing = false;
            this.currentImage = craftImages[0];
        }

        this.currentImage = craftImages[index / freq];
    }

    public void fire() throws IOException {
        this.firing = true;
        plasmaList.add(new Plasma(angle));
    }

    public boolean hitMine(Mine mine) {
        if (mine.getY() > y) {
            return true;
        }

        return false;
    }
    /**
     * 当键入的字母没有地雷的单词能对应时，用黄色飞船背景提示
     */
    public void noMineTips() {
        if (noLetter) { // 如果是，则进行提示
            int freq = 3;
            if (tipsIndex < craftTipsImages.length * freq) {
                this.currentImage = craftTipsImages[tipsIndex / freq];
                tipsIndex++;
            } else {
                noLetter = false;
                tipsIndex = 0;
                this.currentImage = craftImages[0];
            }

        }
    }


    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public List<Plasma> getPlasmaList() {
        return plasmaList;
    }

    public void setNoLetter(boolean noLetter) {
        this.noLetter = noLetter;
    }

    public void setAngle(double angle) {
        this.angle = angle;
    }
}
